from typing import Optional

from tortoise import Tortoise
from tortoise.contrib.pydantic import pydantic_model_creator
from models import Project,ProjectFiles,ProjectMembers,ProjectGroups,ProjectAuditRules,ProjectAuditRecords,ProjectBatches
from pydantic import BaseModel

# 初始化模型关系
Tortoise.init_models(["models"], "models")

Project_Pydantic = pydantic_model_creator(Project,name="Porject")
# 排除只读属性
ProjectIn_Pydantic = pydantic_model_creator(Project,name="PorjectIn",exclude_readonly=True,exclude=["createTime","updateTime","project_batch_id","project_owner_id"])

ProjectFiles_Pydantic = pydantic_model_creator(ProjectFiles,name="ProjectFiles")
ProjectFilesIn_Pydantic = pydantic_model_creator(ProjectFiles,name="ProjectFilesIn",exclude_readonly=True,exclude=["createTime","updateTime"])

# 项目统计信息
class ProjectStatistics(BaseModel):
    title: str
    unit: str
    value: str
    subTitle: str
    subValue: str
    
ProjectMembers_Pydantic = pydantic_model_creator(ProjectMembers,name="ProjectMembers_Pydantic",exclude_readonly=True,exclude=["project","project_id","createTime","updateTime"])

class ProjectInWithId_Pydantic(ProjectIn_Pydantic):
    id: Optional[int] = None
    
class ProjectMembersWithId_Pydantic(ProjectMembers_Pydantic):
    id: Optional[int] = None
    key: int
    
    
ProjectGroups_Pydantic = pydantic_model_creator(ProjectGroups,name="ProjectGroups")

ProjectAuditRules_Pydantic = pydantic_model_creator(ProjectAuditRules,name="ProjectAuditRules",exclude=["createTime","updateTime"])
ProjectAuditRulesIn_Pydantic = pydantic_model_creator(ProjectAuditRules,name="ProjectAuditRulesIn",exclude_readonly=True,exclude=["rule_name","createTime","updateTime"])

ProjectAuditRecords_Pydantic = pydantic_model_creator(ProjectAuditRecords,name="ProjectAuditRecords")
ProjectAuditRecordsIn_Pydantic = pydantic_model_creator(ProjectAuditRecords,name="ProjectAuditRecordsIn",exclude_readonly=True,exclude=["createTime","updateTime","project_id","user_id"])

class ProjectAuditRecordsAll_Pydantic(ProjectAuditRecords_Pydantic):
    user_name: str
    project_name: str
    
ProjectBatches_Pydantic = pydantic_model_creator(ProjectBatches,name="ProjectBatches")
ProjectBatchesIn_Pydantic = pydantic_model_creator(ProjectBatches,name="ProjectBatchesIn",exclude_readonly=True,exclude=["createTime","updateTime"])

# 项目搜索项
class ProjectQuery(BaseModel):
    statu: Optional[str] = None
    project_group: Optional[str] = None
    project_batch: Optional[str] = None
    query_str: Optional[str] = None